home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!russell
- From: russell@news.mdli.com (Russell Blackadar)
- Newsgroups: comp.lang.c++
- Subject: Re: vietual data in C++?
- Date: 16 Jan 1996 02:24:14 GMT
- Organization: HoloNet National Internet Access System: 510-704-1058/modem
- Message-ID: <4df28e$2i7@colossus.holonet.net>
- References: <4d94cn$9vo@fnnews.fnal.gov>
- NNTP-Posting-Host: jubal.mdli.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Oleg Krivosheev (kriol) wrote:
- : Hi, Gentlemen
-
- : i want to implement something like virtual data (if such an idiom
- : is already known, there must be it's own name)...
-
- Look up "virtual data" in the c.l.c++ FAQ; you'll find a very
- interesting discussion of this idiom. It's useful when objects
- in one class hierarchy need to contain (or, as it's implemented,
- point to) a member of a different hierarchy. It's not clear to
- me that your situation falls into this category.
-
- [code deleted]
-
- : 2. What's wrong with above snippet?
-
- In an earlier post, David Bryden pointed out some dubious things
- in your code. Let me add I'm not happy with your idea of deriving
- unrelated classes from a struct. This is using an OOP construct
- for a very non-OO purpose, and the result will be an unmaintainable
- C/C++ mishmash. It would be better to code the struct as a member
- of the classes, and IMO even better to abandon the struct and code
- meaningful data members for each class, separately. (And make them
- private!) Inheritance doesn't seem appropriate here.
-
- As long as you use a template, the question of inheritance is moot
- anyway, and "virtual data" doesn't apply. For example, you could
- have the Base template contain an object, rather than a pointer:
-
- template <class T> Base { ...
- T myinstance; // instead of T* pVirtualData;
- };
-
- Then Base<Data1> and Base<Data2> would work fine as base classes
- for D1 and D2, if you want, and you could write:
-
- D1 d1;
- cerr << d1.myinstance.i1;
-
- (Of course, you'd do this differently since you took my advice
- and made i1 private, right? :)
-
- BTW, if Base doesn't have to be a template for other reasons,
- and you want to keep the BaseClass/Data1/Data2... hierarchy (ugh),
- then perhaps the "virtual data" idiom would work in lieu of a
- template. You definitely don't need both.
-
- Good luck.
- --
- Russell Blackadar, russell@mdli.com
-